<?php


namespace app\cms\admin\rpc\controller;


use app\cms\community\lib\saas\buildNginxConfig;
use app\cms\model\saas\saasClientModel;
use iflow\Router\implement\Request\GetMapping;
use iflow\swoole\implement\Commounity\Rpc\Request\Routers\RpcController;

#[RpcController('rpc/saas/account')]
class accountController {

    // SAAS服务验证控制器
    #[GetMapping]
    public function validAccount(string $domain): array|string
    {
        $account = (new saasClientModel()) -> getSaasServerInfoByDomain($domain);
        if (!$account) return "当前应用服务不存在";

        if ($account['lease_renewal_end_time'] < time()) {
            return  "当前应用服务已过期";
        }

        if ($account['lease_renewal_time'] > time()) {
            return  "当前应用服务暂未生效";
        }
        return $account;
    }

    // 重启Nginx
    #[GetMapping]
    public function reloadNginxServer(string $domain, string $nginxConfig, array $ssl = []): string
    {
        $build = new buildNginxConfig();

        $reload = $build -> setConfig($nginxConfig)
            -> setServerName($domain)
            -> setSsl($ssl)
            -> saveConfig()
            -> reload();
        return $reload ? 'OK' : 'Fail';
    }
}
